package com.hontech.bv.http

import android.content.Intent
import android.net.Uri
import com.hontech.bv.app.AppContext
import com.hontech.bv.utils.API_BASE_URL
import com.hontech.bv.utils.parseJson
import kotlinx.serialization.SerialName


@kotlinx.serialization.Serializable
data class FirmInfo(
    val id: Int,

    @SerialName("create_timestamp")
    val createTimestamp: Long,

    val name: String,

    val version: String,

    val describe: String
)

suspend fun queryFirm(typeId: Int): ArrayList<FirmInfo> {
    val buf = Http.get("/api/firm/query?type_id=${typeId}")
    val ret = parseJson<ArrayList<FirmInfo>>(buf)
    ret.sortBy { -it.createTimestamp }
    return ret
}

suspend fun downloadFirm(id: Int): ByteArray {
    return Http.get("/api/firm/download?id=${id}")
}

fun downloadForBrowser(id: Int) {
    val url = "${API_BASE_URL}/api/firm/download?id=${id}"
    val uri = Uri.parse(url)
    val intent = Intent(Intent.ACTION_VIEW)
    intent.data = uri
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    AppContext.startActivity(intent)
}

